﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BTN04.DTO;
using BTN04.BUS;

namespace BTN04.USER
{
    public partial class GioHang : System.Web.UI.Page
    {
        float tongtien = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDS();
        }
        public void LoadDS()
        {
            gvGioHang.DataSource = CurrentContext.Cart.Item;
            gvGioHang.DataBind();
            lbTong.Text = "Tổng tiền: " + string.Format("{0:#,#.##}", tongtien);
        }
 
        protected void gvGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            SanPhamDTO spDTO = new SanPhamDTO();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int masp = Convert .ToInt32 (gvGioHang .DataKeys[e.Row .RowIndex ].Value);
                spDTO = SanPhamBUS.LaySanPhamTheoMa(masp);
                e.Row.Cells[0].Text = spDTO.TenSanPham .ToString ();
                e.Row.Cells[1].Text = string.Format("{0:#,#.##}",spDTO.GiaBan );
                int soluong = Convert.ToInt32((e.Row .Cells [2].FindControl ("txtSoLuong") as TextBox ).Text);
                float thanhtien = spDTO.GiaBan * soluong;
                e.Row.Cells[3].Text = string.Format("{0:#,#.##}", thanhtien);
                tongtien += thanhtien;
            }
        }

        protected void gvGioHang_DataBound(object sender, EventArgs e)
        {
            lbTong.Text ="Tổng tiền"+ string.Format("{0:#,#.##}", tongtien );
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvGioHang.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    int masp = Convert.ToInt32(gvGioHang .DataKeys [row.RowIndex ].Value );
                    int soluong = Convert.ToInt32((row.Cells [2].FindControl("txtSoLuong") as TextBox).Text);
                    CurrentContext.Cart.UpdateItem(masp ,soluong);
                    row.Cells[2].Text = soluong.ToString();
                }
            }
            LoadDS();
        }

        protected void gvGioHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int masp = Convert.ToInt32(gvGioHang.DataKeys[e.RowIndex].Value);
            CurrentContext.Cart.RemoveItem(masp );
            LoadDS();
        }
    }
}
